/**
 * @author renato
 * Factory responsable of receiving the user data
 * Created on 28/04/2006
 * Updated on 02/04/2010
 */
package br.unb.plugins.geral.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.unb.plugins.geral.model.GeneratorModel;
import br.unb.plugins.geral.model.XmlComponentModel;
import br.unb.plugins.geral.model.XmlComboItemModel;
import br.unb.plugins.geral.model.XmlFrameModel;
import br.unb.plugins.geral.validation.ValidateException;
import br.unb.utils.Console;
import br.unb.utils.Resource;
import java.util.Map.Entry;

public class XmlFrame extends JFrame {

    private HashMap<String, XmlComponent> componentsMap;
    private GeneratorModel genModel;
    private boolean generatorReady = false;
    //
    private JPanel principal = new JPanel(new GridBagLayout());
    private JPanel descriptionPanel = new JPanel();
    private JLabel descriptinLabel = new JLabel();
    private static final Dimension defaultDimension = new Dimension(367, 175);
    private boolean xmlValid = false;

    public XmlFrame() {
        super();
        descriptionPanel.add(descriptinLabel);
        principal.removeAll();
    }

    public void init(XmlComboItemModel comboItem) {
        if (comboItem == null) {
            this.dispose();
            return;
        } else {
            componentsMap = null;
            componentsMap = new HashMap();
            genModel = null;
        }

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(1, 1, 1, 1);
        c.gridx = 0;
        c.gridy = 0;

        XmlFrameModel model = (XmlFrameModel) XmlFactory.getFrameFromXml(comboItem.getLocation());
        if (model != null) {
            xmlValid = true;
            componentsMap.clear();
            descriptinLabel.setText(comboItem.getDescription());
            principal.removeAll();

            this.setName(comboItem.getDescription());
            this.setSize(defaultDimension);
            this.genModel = new GeneratorModel((String) model.getClassName());

            for (Object line : model.getPanel()) {
                c.gridx = 0;
                for (Object entry : (ArrayList) line) {
                    XmlComponentModel componentModel = ((XmlComponentModel) entry);
                    XmlComponent tmp = new XmlComponent();
                    c.anchor = componentModel.getAlignment();
                    principal.add(tmp.createXmlComponent(componentModel, this), c);
                    c.gridx++;
                    componentsMap.put(tmp.getData().getName(), tmp);
                }
                c.gridy++;
            }

        } else {
            xmlValid = false;
            componentsMap.clear();
            genModel = null;
            descriptinLabel.setText("Xml not Found!");
            this.setName("Xml not Found!");
            this.setSize(defaultDimension);
        }
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(descriptionPanel, BorderLayout.NORTH);
        this.getContentPane().add(principal, BorderLayout.CENTER);
        this.setBounds(430, 340, 580, 440);
        this.pack();
        this.setVisible(true);
    }

    public boolean setParameters() {
        if (!xmlValid) {
            //Console.println("load a valid xml first");
            return false;
        }

        String validationMessage = "";
        for (Entry<String, XmlComponent> entry : componentsMap.entrySet()) {
            try {
                //String key = entry.getKey();
                XmlComponent x = entry.getValue();
                if (x.isParameter()) {
                    x.validate();
                }
            } catch (ValidateException e) {
                validationMessage += e.getResult();
            } catch (Exception e) {
                e.printStackTrace();
                Console.printError(e.getMessage());
                Console.printError(
                        Resource.getString("UNKNOW_ERROR_VALIDATING")
                        + Resource.getString("CHANGES_NOT_APPLIED"));
                return false;
            }
        }
        //Displays the validation results
        if (!validationMessage.isEmpty()) {
            Console.showMessageDialog(validationMessage);
            return false;
        } else {
            for (Entry<String, XmlComponent> entry : componentsMap.entrySet()) {
                XmlComponent x = entry.getValue();
                if (x.isParameter()) {
                    genModel.setParameter(x.getData().getName(), ((JTextField) x.getComponent()).getText());
                    generatorReady = true;
                }
            }
            return true;
        }
    }

    public boolean isGenaratorReady() {
        return generatorReady;
    }

    public HashMap getComponentsMap() {
        return componentsMap;
    }

    public GeneratorModel getGenModel() {
        return genModel;
    }
}
